﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernateToSQLMapping.DAL;
using System.Collections;

namespace NHibernateToSQLMapping.Factory
{
    public class MasterFactory : IMasterFactory
    {
        ArrayList s_PrefixList;
        /// <summary>
        /// 初始化实体类列表
        /// </summary>
        private ArrayList PrefixList
        {
            get
            {
                if (s_PrefixList == null)
                {
                    ArrayList prefixList = new ArrayList();
                    prefixList.Add(new MasterDALElement("SQL Server 2008", new SQLServer2008DAL()));
                    prefixList.Add(new MasterDALElement("SQL Server 2005", new SQLServer2008DAL()));
                    prefixList.Add(new MasterDALElement("SQL Server 2000", new SQLServer2000DAL()));
                    s_PrefixList = prefixList;
                }
                return s_PrefixList;
            }
            set
            {
                s_PrefixList = value;
            }
        }

        /// <summary>
        /// 创建实体类
        /// </summary>
        /// <param name="name"></param>
        /// <param name="useUriBase"></param>
        /// <returns></returns>
        private IMasterDAL CreateDAL(string name, bool useUriBase)
        {
            bool Found = false;
            MasterDALElement Current = null;
            ArrayList prefixList = PrefixList;
            for (int i = 0; i < prefixList.Count; i++)
            {
                Current = (MasterDALElement)prefixList[i];
                if (name == Current.Name)
                {
                    Found = true;
                    break;
                }
            }
            if (Found)
            {
                return Current.Creator;
            }
            else
            {
                throw new NotSupportedException("IMasterDAL");
            }
        }

        /// <summary>
        /// 创建实体类
        /// </summary>
        /// <param name="name">实体类对应名</param>
        /// <returns></returns>
        public IMasterDAL CreateDAL(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            return CreateDAL(name, false);
        }

        /// <summary>
        /// 注册新的实体类
        /// </summary>
        /// <param name="prefix">名称</param>
        /// <param name="creator">实体类</param>
        public void RegisterPrefix(string prefix, IMasterDAL creator)
        {
            if (prefix == null)
            {
                throw new ArgumentNullException("prefix");
            }
            if (creator == null)
            {
                throw new ArgumentNullException("creator");
            }
            MasterDALElement Current = null;
            ArrayList prefixList =(ArrayList)PrefixList.Clone();
            for (int i = 0; i < prefixList.Count; i++)
            {
                Current = (MasterDALElement)prefixList[i];
                if (prefix == Current.Name)
                {
                    prefixList.Remove(Current);
                    break;
                }
            }
            prefixList.Add(new MasterDALElement(prefix, creator));
            PrefixList = prefixList;
        }
    }

    /// <summary>
    /// 实体类名称对应
    /// </summary>
    internal class MasterDALElement
    {
        public string Name;
        public IMasterDAL Creator;
        public MasterDALElement(string N, IMasterDAL C)
        {
            Name = N;
            Creator = C;
        }
    }
}
